package com.fantasy.httpunit;

import java.io.IOException;
import java.net.MalformedURLException;

import com.gargoylesoftware.htmlunit.BrowserVersion;
import com.gargoylesoftware.htmlunit.FailingHttpStatusCodeException;
import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.html.DomElement;
import com.gargoylesoftware.htmlunit.html.HtmlPage;

public class HtmlUnitTest {
	
	public static void main(String[] args) {
		try {
			testUserHtmlUnit();
		} catch (FailingHttpStatusCodeException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (MalformedURLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public static void testUserHtmlUnit() throws FailingHttpStatusCodeException,
			MalformedURLException, IOException {

		/** HtmlUnit请求web页面 */
		@SuppressWarnings("resource")
		WebClient wc = new WebClient(BrowserVersion.CHROME);
		wc.getOptions().setUseInsecureSSL(true);
		wc.getOptions().setJavaScriptEnabled(true); // 启用JS解释器，默认为true
		wc.getOptions().setCssEnabled(true); // 禁用css支持
		wc.getOptions().setThrowExceptionOnScriptError(false); // js运行错误时，是否抛出异常
		wc.getOptions().setTimeout(100000); // 设置连接超时时间 ，这里是10S。如果为0，则无限期等待
		wc.getOptions().setDoNotTrackEnabled(false);
		HtmlPage page = wc.getPage("http://www.macauslot.com/slot/html/result/livescore.htm?sport=FB&lang=TW");
		wc.setJavaScriptTimeout(5000);
		System.out.println(page.asText());
		System.out.println("****************--------------------------");
		DomElement domElem = page.getElementById("#tableMain");
		System.out.println(domElem.asText());
		
/*		DomNodeList<DomElement> links = page.getElementsByTagName("a");

		for (DomElement link : links) {
			System.out.println(link.asText() + "  " + link.getAttribute("href"));
		}*/
	}
}
